package qiutan

import (
	"strconv"
	"strings"
	"whoscored/util/boltdb"
)
func leagueData2(text string) (bool,[]float64) {

	s:=strings.Split(text,"$$$$")

	ll:=strings.Split(s[0],"$$")

	if len(ll)<2||!strings.Contains(ll[0],"^")||!strings.Contains(ll[1],"^"){
		return false,nil
	}

	home:=strings.Split(ll[0],"!")
	away:=strings.Split(ll[1],"!")


	//主队数据
	homeAllSlice:=strings.Split(home[0],"^")

	//主队主场数据
	homeHomeSlice:=strings.Split(home[1],"^")


	//客场数据
	awayAllSlice:=strings.Split(away[0],"^")

	//客队客场数据
	awayAwaySlice:=strings.Split(away[2],"^")

	//主队主场得分能力
	homeHomeWinScore,_:=strconv.ParseFloat(homeHomeSlice[5],64)
	homeHomeLossScore,_:=strconv.ParseFloat(homeHomeSlice[6],64)
	homeHomeScore:=float64(0)
	if homeHomeWinScore+homeHomeLossScore!=0{
		homeHomeScore=homeHomeWinScore/(homeHomeWinScore+homeHomeLossScore)
	}


	//客队客场得分能力
	awayAwayWinScore,_:=strconv.ParseFloat(awayAwaySlice[5],64)
	awayAwayLossScore,_:=strconv.ParseFloat(awayAwaySlice[6],64)
	awayAwayScore:=float64(0)
	if awayAwayWinScore+awayAwayLossScore!=0{
		awayAwayScore=awayAwayWinScore/(awayAwayWinScore+awayAwayLossScore)
	}


	//联赛排名
	homeRank,_:=strconv.ParseFloat(homeAllSlice[8],64)
	awayRank,_:=strconv.ParseFloat(awayAllSlice[8],64)

	return true,[]float64{
		homeHomeScore,awayAwayScore,
		homeRank-awayRank,
	}
}
func asiaData(data string) ([]float64) {
	asiaSlice:=strings.Split(data,"!")

	l:=int(0)
	//初盘均值
	s1:=float64(0)
	//终盘均值
	s2:=float64(0)
	oddsData:=make(map[string][]string)
	asia0:=make(map[string]int64)
	asia:=make(map[string]int64)
	for _,v:=range asiaSlice{
		asiaOdds:=strings.Split(v,"^")
		if (len(asiaOdds)<7){
			continue
		}
		name:=asiaOdds[0]
		odds,_:=strconv.ParseFloat(asiaOdds[3],64)
		odds2,_:=strconv.ParseFloat(asiaOdds[6],64)
		s1=s1+odds
		s2=s2+odds2

		//以终盘中大众盘口作为最终终盘口
		if asia[asiaOdds[6]]==0{
			asia[asiaOdds[6]]=1
		}else{
			asia[asiaOdds[6]]=asia[asiaOdds[6]]+1
		}
		if asia0[asiaOdds[3]]==0{
			asia0[asiaOdds[3]]=1
		}else{
			asia0[asiaOdds[3]]=asia0[asiaOdds[3]]+1
		}
		oddsData[name]=asiaOdds
		l++
	}


	//初盘
	n0:=float64(-9999)
	temp0:=int64(0)
	//终盘
	n:=float64(-9999)
	temp:=int64(0)
	for k,v:=range asia0{
		if v>temp0{
			temp0=v
			n0,_=strconv.ParseFloat(k,64)

		}
	}
	for k,v:=range asia{
		if v>temp{
			temp=v
			n,_=strconv.ParseFloat(k,64)

		}
	}
	return []float64{n0,n,s1/float64(l),s2/float64(l)}
}
func overunderData(data string) (bool,[]float64)  {
	slice:=strings.Split(data,"!")
	f1:=float64(0)
	f2:=float64(0)
	for _,v:=range slice{
		odds:=strings.Split(v,"^")
		if (len(odds)<7){
			continue
		}
		name:=odds[0]

		if name=="澳门"{
			f1,_=strconv.ParseFloat(odds[3],64)
			f2,_=strconv.ParseFloat(odds[6],64)
			break
		}
	}

	if f1==0{
		return false,nil
	}
	return true,[]float64{f1,f1-f2}
}
func OverunderFormat(startAt string,matchId int64,league string,home string,away string,fn func([][]float64))  {
	k:=strconv.FormatInt(matchId,10)
	year:=strings.Split(startAt,"-")[0]
	boltdb.Get("asia-"+year,league,k, func(asia string) {
		boltdb.Get("overunder-"+year, league, k, func(text string) {
			d1:=asiaData(asia)
			//b2,d2:=leagueData2(text)
			b3,d3:=overunderData(text)
			if !b3{
				return
			}
			s:=[]float64{d1[2],d1[2]-d1[3]}
			fn([][]float64{s,d3})
		})

	})
}